package com.gly091020.util;

import com.gly091020.EnglishFight;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

public class EnglishWordPractice {
    private static Map<String, String> wordMap = new HashMap<>();
    private static Random random = new Random();

    // 初始化加载单词库
    static {
        loadWords();
    }

    /**
     * 从JSON文件加载单词和翻译
     */
    private static void loadWords() {
        Path filePath = FabricLoader.getInstance().getConfigDir().resolve(EnglishFight.ModID + "Files").resolve(EnglishFight.config.fileName);
        try {
            if (Files.exists(filePath)) {
                String json = Files.readString(filePath);
                wordMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>(){}.getType());
            } else {
                // 默认单词库
                wordMap.put("red", "红");
                wordMap.put("yellow", "黄");
                wordMap.put("green", "绿");
            }
        } catch (IOException e) {
            System.err.println("加载单词文件失败: " + e.getMessage());
        }
    }

    /**
     * 随机生成一个题目
     * @param isWordToTranslation true=单词到翻译, false=翻译到单词
     */
    public static EnglishFightQuestion generateQuestion(boolean isWordToTranslation) {
        if (wordMap.isEmpty()) {
            throw new IllegalStateException("单词库未加载或为空");
        }

        // 随机选择正确答案
        List<String> keys = new ArrayList<>(wordMap.keySet());
        String correctKey = keys.get(random.nextInt(keys.size()));
        String correctValue = wordMap.get(correctKey);

        // 创建题目对象
        EnglishFightQuestion question = new EnglishFightQuestion();

        // 设置题目和选项
        if (isWordToTranslation) {
            question.title = correctKey;

            // 获取两个错误选项
            List<String> wrongValues = wordMap.values().stream()
                    .filter(v -> !v.equals(correctValue))
                    .collect(Collectors.toList());
            Collections.shuffle(wrongValues);

            // 准备三个选项(1正确+2错误)
            List<String> options = new ArrayList<>();
            options.add(correctValue);
            options.add(wrongValues.get(0));
            options.add(wrongValues.get(1));

            // 打乱选项顺序
            question.rightIndex = options.indexOf(correctValue);
            Collections.shuffle(options);
            question.rightIndex = options.indexOf(correctValue); // 重新获取正确索引

            question.items = options;
        } else {
            question.title = correctValue;

            // 获取两个错误选项
            List<String> wrongKeys = wordMap.keySet().stream()
                    .filter(k -> !k.equals(correctKey))
                    .collect(Collectors.toList());
            Collections.shuffle(wrongKeys);

            // 准备三个选项(1正确+2错误)
            List<String> options = new ArrayList<>();
            options.add(correctKey);
            options.add(wrongKeys.get(0));
            options.add(wrongKeys.get(1));

            // 打乱选项顺序
            question.rightIndex = options.indexOf(correctKey);
            Collections.shuffle(options);
            question.rightIndex = options.indexOf(correctKey); // 重新获取正确索引

            question.items = options;
        }

        return question;
    }

    /**
     * 获取所有单词数量
     */
    public static int getWordCount() {
        return wordMap.size();
    }

    /**
     * 重新加载单词库
     */
    public static void reloadWords() {
        wordMap.clear();
        loadWords();
    }
}